home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / SimpleHTTPServer.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  6.9 KB  |  216 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Simple HTTP Server.
  5.  
  6. This module builds on BaseHTTPServer by implementing the standard GET
  7. and HEAD requests in a fairly straightforward manner.
  8.  
  9. '''
  10. __version__ = '0.6'
  11. __all__ = [
  12.     'SimpleHTTPRequestHandler']
  13. import os
  14. import posixpath
  15. import BaseHTTPServer
  16. import urllib
  17. import urlparse
  18. import cgi
  19. import shutil
  20. import mimetypes
  21. from StringIO import StringIO
  22.  
  23. class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  24.     '''Simple HTTP request handler with GET and HEAD commands.
  25.  
  26.     This serves files from the current directory and any of its
  27.     subdirectories.  The MIME type for files is determined by
  28.     calling the .guess_type() method.
  29.  
  30.     The GET and HEAD requests are identical except that the HEAD
  31.     request omits the actual contents of the file.
  32.  
  33.     '''
  34.     server_version = 'SimpleHTTP/' + __version__
  35.     
  36.     def do_GET(self):
  37.         '''Serve a GET request.'''
  38.         f = self.send_head()
  39.         if f:
  40.             self.copyfile(f, self.wfile)
  41.             f.close()
  42.         
  43.  
  44.     
  45.     def do_HEAD(self):
  46.         '''Serve a HEAD request.'''
  47.         f = self.send_head()
  48.         if f:
  49.             f.close()
  50.         
  51.  
  52.     
  53.     def send_head(self):
  54.         '''Common code for GET and HEAD commands.
  55.  
  56.         This sends the response code and MIME headers.
  57.  
  58.         Return value is either a file object (which has to be copied
  59.         to the outputfile by the caller unless the command was HEAD,
  60.         and must be closed by the caller under all circumstances), or
  61.         None, in which case the caller has nothing further to do.
  62.  
  63.         '''
  64.         path = self.translate_path(self.path)
  65.         f = None
  66.         if os.path.isdir(path):
  67.             for index in ('index.html', 'index.htm'):
  68.                 index = os.path.join(path, index)
  69.                 if os.path.exists(index):
  70.                     path = index
  71.                     break
  72.                     continue
  73.             else:
  74.                 return self.list_directory(path)
  75.         
  76.         ctype = self.guess_type(path)
  77.         
  78.         try:
  79.             f = open(path, 'rb')
  80.         except IOError:
  81.             self.send_error(404, 'File not found')
  82.             return None
  83.  
  84.         self.send_response(200)
  85.         self.send_header('Content-type', ctype)
  86.         self.send_header('Content-Length', str(os.fstat(f.fileno())[6]))
  87.         self.end_headers()
  88.         return f
  89.  
  90.     
  91.     def list_directory(self, path):
  92.         '''Helper to produce a directory listing (absent index.html).
  93.  
  94.         Return value is either a file object, or None (indicating an
  95.         error).  In either case, the headers are sent, making the
  96.         interface the same as for send_head().
  97.  
  98.         '''
  99.         
  100.         try:
  101.             list = os.listdir(path)
  102.         except os.error:
  103.             self.send_error(404, 'No permission to list directory')
  104.             return None
  105.  
  106.         list.sort(key = (lambda a: a.lower()))
  107.         f = StringIO()
  108.         displaypath = cgi.escape(urllib.unquote(self.path))
  109.         f.write('<title>Directory listing for %s</title>\n' % displaypath)
  110.         f.write('<h2>Directory listing for %s</h2>\n' % displaypath)
  111.         f.write('<hr>\n<ul>\n')
  112.         for name in list:
  113.             fullname = os.path.join(path, name)
  114.             displayname = linkname = name
  115.             if os.path.isdir(fullname):
  116.                 displayname = name + '/'
  117.                 linkname = name + '/'
  118.             
  119.             if os.path.islink(fullname):
  120.                 displayname = name + '@'
  121.             
  122.             f.write('<li><a href="%s">%s</a>\n' % (urllib.quote(linkname), cgi.escape(displayname)))
  123.         
  124.         f.write('</ul>\n<hr>\n')
  125.         length = f.tell()
  126.         f.seek(0)
  127.         self.send_response(200)
  128.         self.send_header('Content-type', 'text/html')
  129.         self.send_header('Content-Length', str(length))
  130.         self.end_headers()
  131.         return f
  132.  
  133.     
  134.     def translate_path(self, path):
  135.         '''Translate a /-separated PATH to the local filename syntax.
  136.  
  137.         Components that mean special things to the local file system
  138.         (e.g. drive or directory names) are ignored.  (XXX They should
  139.         probably be diagnosed.)
  140.  
  141.         '''
  142.         path = urlparse.urlparse(path)[2]
  143.         path = posixpath.normpath(urllib.unquote(path))
  144.         words = path.split('/')
  145.         words = filter(None, words)
  146.         path = os.getcwd()
  147.         for word in words:
  148.             (drive, word) = os.path.splitdrive(word)
  149.             (head, word) = os.path.split(word)
  150.             if word in (os.curdir, os.pardir):
  151.                 continue
  152.             
  153.             path = os.path.join(path, word)
  154.         
  155.         return path
  156.  
  157.     
  158.     def copyfile(self, source, outputfile):
  159.         '''Copy all data between two file objects.
  160.  
  161.         The SOURCE argument is a file object open for reading
  162.         (or anything with a read() method) and the DESTINATION
  163.         argument is a file object open for writing (or
  164.         anything with a write() method).
  165.  
  166.         The only reason for overriding this would be to change
  167.         the block size or perhaps to replace newlines by CRLF
  168.         -- note however that this the default server uses this
  169.         to copy binary data as well.
  170.  
  171.         '''
  172.         shutil.copyfileobj(source, outputfile)
  173.  
  174.     
  175.     def guess_type(self, path):
  176.         """Guess the type of a file.
  177.  
  178.         Argument is a PATH (a filename).
  179.  
  180.         Return value is a string of the form type/subtype,
  181.         usable for a MIME Content-type header.
  182.  
  183.         The default implementation looks the file's extension
  184.         up in the table self.extensions_map, using application/octet-stream
  185.         as a default; however it would be permissible (if
  186.         slow) to look inside the data to make a better guess.
  187.  
  188.         """
  189.         (base, ext) = posixpath.splitext(path)
  190.         if ext in self.extensions_map:
  191.             return self.extensions_map[ext]
  192.         
  193.         ext = ext.lower()
  194.         if ext in self.extensions_map:
  195.             return self.extensions_map[ext]
  196.         else:
  197.             return self.extensions_map['']
  198.  
  199.     if not mimetypes.inited:
  200.         mimetypes.init()
  201.     
  202.     extensions_map = mimetypes.types_map.copy()
  203.     extensions_map.update({
  204.         '': 'application/octet-stream',
  205.         '.py': 'text/plain',
  206.         '.c': 'text/plain',
  207.         '.h': 'text/plain' })
  208.  
  209.  
  210. def test(HandlerClass = SimpleHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer):
  211.     BaseHTTPServer.test(HandlerClass, ServerClass)
  212.  
  213. if __name__ == '__main__':
  214.     test()
  215.  
  216.